import actionTypes from "../action";
import { checkEmptyEdit, checkNumber, checkImage, checkPrice } from "./rules";
import { OPTION_GOODS_TYPE, OPTION_RESULT } from "../options";
/**
 * 特殊字段说明
 * @param {string} upKey type为image时使用，用于存储oss fileKey字段
 * @param {Boolean} repeat type为image时使用，是否可多图
 *
 * @param {Array} accept type为image时， 指定可上传的图片格式
 * @param {string} auto_dispatch 加载时触发的action
 *
 * @param {Function} check_value type为${type}时使用，初始化时传入data.type的值，返回想要的值
 *
 * @param {string} cascader 指定级联操作的下级，当value改变时会触发下级的dispatch
 * @param {string} dispatch 级联操作时，上级value改变时触发的action
 *
 * @param {Number} maxLength type为textarea时最大length，默认为500
 *
 * @param {string} greater 用于表单校验，该字段的值必须大于greater指定字段的值
 * @param {string} equal 用于表单校验，该字段的值必须等于equal指定字段的值
 */
export default {
  [actionTypes.CATEGORY_ICON]: {
    title: "分类栏",
    form: [
      {
        type: "image",
        prop: "pictureUrl",
        upKey: "pictureFileKey",
        label: "图标",
        rule: [
          {
            required: true,
            validator: checkImage,
            trigger: "change",
          },
        ],
      },
      {
        type: "text",
        prop: "name",
        label: "名称",
        placeholder: "请输入分类名称",
        rule: {
          required: true,
          message: "请填写名称",
          trigger: "blur",
        },
      },
      {
        type: "number",
        prop: "sort",
        label: "排序",
        isSort: true,
        auto_dispatch: "dic/getCategoryMaxSort",
        rule: {
          required: true,
          message: "请输入序号",
          trigger: "change",
        },
        option(_, $store) {
          return $store.getters["dic/categoryMaxSort"];
        },
      },
    ],
  },
  [actionTypes.GOODS_CATEGORY_FIRST]: {
    title: "一级分类",
    form: [
      {
        type: "text",
        prop: "categoryName",
        label: "一级分类名称",
        placeholder: "请输入分类名称",
        rule: [
          {
            required: true,
            message: "请填写名称",
            trigger: "blur",
          },
          {
            max: 10,
            message: "一级分类名称不能超过10个字",
            trigger: "blur",
          },
        ],
      },
      {
        type: "textarea",
        prop: "body",
        label: "一级分类描述",
        placeholder: "请输入描述",
        rule: [
          {
            required: true,
            message: "请填写一级分类描述",
            trigger: "blur",
          },
        ],
        maxLength: 60,
      },
      {
        type: "select",
        prop: "isShow",
        label: "前端显示二级分类",
        rule: {
          required: true,
          message: "请选择是否显示二级分类",
          trigger: "blur",
        },
        option: () => OPTION_RESULT,
      },
      {
        type: "number",
        prop: "sort",
        label: "排序",
        isSort: true,
        auto_dispatch: "dic/getFirstCategoryMax",
        rule: {
          required: true,
          message: "请输入排序",
          trigger: "change",
        },
        // option: (_, $store) => {
        //   return $store.getters["dic/firstCategoryMax"];
        // },
      },
      {
        type: "image",
        prop: "pictureUrl",
        upKey: "pictureFileKey",
        label: "图片",
        show(formData) {
          return !!formData.isIndexShow;
        },
        rule: [
          {
            required: true,
            validator: checkImage,
            trigger: "change",
          },
        ],
      },
      {
        type: "select",
        prop: "isIndexShow",
        label: "首页展示",
        rule: {
          required: true,
          message: "请选择是否首页展示",
          trigger: "change",
        },
        option: () => OPTION_RESULT,
      },
      {
        type: "number",
        prop: "indexSort",
        label: "首页排序",
        isSort: true,
        show(formData) {
          return !!formData.isIndexShow;
        },
        auto_dispatch: "dic/getFirstCategoryIndexMax",
        rule: {
          required: true,
          message: "请输入首页排序",
          trigger: "change",
        },
        // option: (_, $store) => {
        //   return $store.getters["dic/firstCategoryIndexMax"];
        // },
      },
    ],
  },
  [actionTypes.GOODS_CATEGORY_SECOND]: {
    title: "二级分类",
    form: [
      {
        type: "text",
        prop: "categoryName",
        label: "二级分类",
        placeholder: "请输入二级分类名称",
        rule: [
          {
            required: true,
            message: "请填写二级分类名称",
            trigger: "blur",
          },
          {
            max: 10,
            message: "字数不能超过10个",
            trigger: "blur",
          },
        ],
      },
      {
        type: "image",
        prop: "pictureUrl",
        upKey: "pictureFileKey",
        label: "二级分类图片",
        rule: [
          {
            required: true,
            validator: checkImage,
            trigger: ["change", "blur"],
          },
        ],
      },
      {
        type: "select",
        prop: "parentId",
        label: "属于",
        auto_dispatch: "dic/getFirstCategoryList",
        rule: {
          required: true,
          message: "请选择属于某个一级分类",
          trigger: "blur",
        },
        option: (_, $store) => {
          return $store.getters["dic/firstCategoryList"];
        },
      },
    ],
  },
  [actionTypes.GOODS_MANAGE]: {
    title: "商品详情",
    form: [
      {
        type: "image",
        prop: "pictureUrl",
        upKey: "pictureFileKey",
        label: "商品图片",
        repeat: true,
        accept: ["jpg"],
        rule: [
          {
            required: true,
            validator: checkImage,
            trigger: ["change", "blur"],
          },
        ],
      },
      {
        type: "text",
        prop: "productName",
        label: "商品名称",
        placeholder: "请输入商品名称",
        rule: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur",
          },
          {
            max: 20,
            message: "商品名称不能超过20个字",
            trigger: "blur",
          },
        ],
      },
      {
        type: "text",
        prop: "specification",
        label: "商品规格",
        placeholder: "请输入商品规格",
        rule: {
          required: true,
          message: "请输入商品规格",
          trigger: "blur",
        },
      },
      {
        type: "number",
        prop: "productCode",
        label: "商品编号",
        edit_value(value) {
          return Number(value);
        },
        placeholder: "请输入编号",
        rule: {
          required: true,
          validator: checkNumber,
          trigger: "blur",
        },
      },
      {
        type: "select",
        prop: "productType",
        label: "商品类型",
        rule: {
          required: true,
          message: "请选择商品类型",
          trigger: "change",
        },
        option: (data) => {
          if (data.firstCategoryId && data.firstCategoryId === 24) {
            return [OPTION_GOODS_TYPE[1]];
          }
          return OPTION_GOODS_TYPE;
        },
      },
      {
        type: "sub-text",
        prop: "price",
        label: "售价",
        placeholder: "请输入价格",
        rule: {
          required: true,
          validator: checkPrice,
          trigger: "blur",
        },
        option: (current) => {
          if (current.productType === "LEASE") {
            return "/日";
          }
          return "";
        },
      },
      {
        type: "number",
        prop: "cashPledge",
        label: "押金",
        placeholder: "请输入押金",
        rule: {
          required: true,
          validator: checkNumber,
          trigger: "blur",
        },
        show: (current) => {
          return current.productType === "LEASE";
        },
      },
      {
        type: "select",
        prop: "isHot",
        label: "热门商品",
        rule: {
          required: true,
          message: "请选择是否为热门商品",
          trigger: "change",
        },
        option: () => {
          return OPTION_RESULT;
        },
      },
      {
        type: "select",
        prop: "isRecommend",
        label: "推荐商品",
        rule: {
          required: true,
          message: "请选择是否为推荐商品",
          trigger: "change",
        },
        option: () => {
          return OPTION_RESULT;
        },
      },
      {
        type: "select",
        prop: "isPutAway",
        label: "是否上架",
        show(_, type) {
          return type === "view";
        },
        option: () => {
          return OPTION_RESULT;
        },
      },
      {
        type: "checkbox",
        prop: "${cityDTO}",
        check_value(list = []) {
          return list
            .filter((i) => i.relevanceType === "CITY")
            .map((item) => {
              return {
                id: item.relevanceId,
                relevanceName: item.relevanceName,
              };
            });
        },
        check_type: "CITY",
        label: "支持城市",
        auto_dispatch: "dic/getUsefulCity",
        cascader: "${exhibitionDTO}",
        option: (_, $store) => {
          return $store.getters["dic/usefulCity"];
        },
        rule: [
          {
            required: true,
            message: "请选择至少一个城市",
            trigger: "change",
          },
        ],
      },
      {
        type: "checkbox",
        prop: "${exhibitionDTO}",
        label: "支持展会",
        check_value(list = []) {
          return list
            .filter((i) => i.relevanceType === "EXHIBITION")
            .map((item) => {
              return {
                id: item.relevanceId,
              };
            });
        },
        rule: [
          {
            required: true,
            message: "请选择至少一个展会",
            trigger: "change",
          },
        ],
        show(_, type, $store) {
          return $store.getters["dic/usefulExhibition"].length > 0;
        },
        check_type: "EXHIBITION",
        dispatch: "dic/getUsefulExhibition",
        option: (_, $store) => {
          return $store.getters["dic/usefulExhibition"];
        },
      },
      {
        type: "select",
        label: "一级分类",
        prop: "firstCategoryId",
        auto_dispatch: "dic/getFirstCategoryList",
        auto_payload: "all",
        cascader: "categorySonId",
        option(_, $store) {
          return $store.getters["dic/firstCategoryList"];
        },
        assignValue: {
          ["productType:24"]: "NOT_LEASE",
        },
        rule: [
          {
            required: true,
            message: "请选择至少一个分类",
            trigger: "change",
          },
        ],
      },
      {
        type: "select",
        label: "二级商品分类",
        prop: "categorySonId",
        dispatch: "dic/getSecondCategoryList",
        check_type: "two",
        show(_, type, $store) {
          return $store.getters["dic/secondCategoryList"].length > 0;
        },
        option(_, $store) {
          return $store.getters["dic/secondCategoryList"];
        },
        rule: [
          {
            required: true,
            message: "请选择至少一个分类",
            trigger: "change",
          },
        ],
      },
      {
        type: "edit",
        prop: "introduce",
        label: "商品介绍",
        rule: {
          required: true,
          validator: checkEmptyEdit,
          trigger: "change",
        },
      },
    ],
  },
};
